<template>
  <a-modal v-model:open="visible" title="业务员列表" width="80%" :footer="null" @cancel="handleCancel">
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-form-item label="姓名" class="smart-query-form-item">
          <a-input style="width: 200px" v-model:value="queryForm.nameLeftLike" placeholder="姓名" />
        </a-form-item>
        <a-form-item label="状态" class="smart-query-form-item">
          <a-select style="width: 200px" v-model:value="queryForm.status" placeholder="状态" :options="SALESMAN_STATUS" />
        </a-form-item>
        <a-form-item label="授权业务" class="smart-query-form-item">
          <a-select style="width: 200px" v-model:value="queryForm.authBussType" placeholder="授权业务" :options="AGENT_SALESMAN_AUTH_BUSS_TYPE" />
        </a-form-item>
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="fetchMerchantData">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item>
      </a-row>
    </a-form>
    <a-table size="small" :dataSource="merchantData" :columns="merchantColumns" rowKey="id" bordered :loading="loading" :pagination="false">
      <template #bodyCell="{ text, column }">
        <!-- 自定义渲染示例 -->
      </template>
    </a-table>
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:page_size="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-modal>
</template>

<script setup>
  import { reactive, ref, h } from 'vue'
  import { message } from 'ant-design-vue'
  import { exhibitionSubPage } from '/@/api/business/b-agent/exhibition/exhibition-api'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const.js'
  import dayjs from 'dayjs'
  import { selectDictLabel } from '/@/utils/common'
  import {
    SALESMAN_STATUS, // 代理商状态
    AGENT_SALESMAN_AUTH_BUSS_TYPE,
  } from '/@/constants/dict'

  const visible = ref(false)
  const loading = ref(false)

  const statusTypeColorArray = ['red', 'blue']

  const queryFormState = {
    nameLeftLike: undefined, //业务员名称
    status: undefined,
    exhibitionId: undefined, //业务员ID
    pageNo: 1,
    pageSize: 10,
  }
  // 总数
  const total = ref(0)
  const queryForm = ref({ ...queryFormState })
  const merchantData = ref([])
  const merchantColumns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      customRender: ({ index }) => {
        return (queryForm.value.pageNo - 1) * queryForm.value.pageSize + index + 1
      },
    },
    {
      title: '姓名',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '授权业务',
      dataIndex: 'authBussType',
      customRender: ({ text }) => {
        return selectDictLabel(AGENT_SALESMAN_AUTH_BUSS_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '手机',
      dataIndex: 'phone',
      ellipsis: true,
    },
    {
      title: '部门',
      dataIndex: 'departName',
      ellipsis: true,
    },
    {
      title: '代理商数量',
      dataIndex: 'agentCount',
      // customRender: ({ text, record }) => {
      //   return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelAgentCount}`)])
      // },
      ellipsis: true,
    },
    {
      title: '总商户数量',
      dataIndex: 'merchantCount',
      customRender: ({ text, record }) => {
        return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelMerchantCount}`)])
      },
      ellipsis: true,
    },
    {
      title: '进件审核中商户数',
      dataIndex: 'registerMerchantCount',
      customRender: ({ text, record }) => {
        return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelRegisterMerchantCount}`)])
      },
      ellipsis: true,
    },
    {
      title: '进件审核通过商户数',
      dataIndex: 'registerPassMerchantCount',
      customRender: ({ text, record }) => {
        return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelRegisterPassMerchantCount}`)])
      },
      ellipsis: true,
    },
    {
      title: '交易商户数',
      dataIndex: 'tradeMerchantCount',
      customRender: ({ text, record }) => {
        return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelTradeMerchantCount}`)])
      },
      ellipsis: true,
    },
    {
      title: '活跃商户数',
      dataIndex: 'activeMerchantCount',
      customRender: ({ text, record }) => {
        return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelActiveMerchantCount}`)])
      },
      ellipsis: true,
    },
    {
      title: '流失商户数',
      dataIndex: 'flowMerchantCount',
      customRender: ({ text, record }) => {
        return h('div', [h('div', `业务:${text}`), h('div', `代理商:${record.towLevelFlowMerchantCount}`)])
      },
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(SALESMAN_STATUS, text)
      },
      ellipsis: true,
    },
  ])

  const emit = defineEmits(['cancel'])

  // 打开弹窗

  function show(record) {
    queryFormState.supervisorId = record?.exhibitionId ? record.exhibitionId : undefined
    queryForm.value.supervisorId = record?.exhibitionId ? record.exhibitionId : undefined
    fetchMerchantData(record)
    visible.value = true
    loading.value = true
  }

  // 关闭弹窗
  function handleCancel() {
    visible.value = false
    emit('cancel')
  }

  // 重置查询条件
  function resetQuery() {
    queryForm.value = { ...queryFormState }
    fetchMerchantData()
  }

  async function fetchMerchantData(record) {
    try {
      const params = {
        ...queryForm.value,
      }
      const data = await exhibitionSubPage(params)
      merchantData.value = data.data
      total.value = data.total
    } catch (error) {
      console.log(error)

      message.error('加载商户信息失败')
    } finally {
      loading.value = false
    }
  }

  defineExpose({
    show,
  })
</script>
